<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin extends CI_Controller{

	public function index(){

		//Kolla om användaren äe inloggad....
		//$this->load->model('credentials_model');
		$query = $this->credentials_model->is_logged_in();

		if($query == null ){
			//När login knappen trycks körs allt nedan
			if($this->input->post('login')){

				$this->load->library('form_validation');
				$this->form_validation->set_rules('username', 'username', 'required|xss_clean');
				$this->form_validation->set_rules('password', 'password', 'required|xss_clean');
				$this->form_validation->set_error_delimiters('<ul><li>','</li></ul>');

			    if($this->form_validation->run() == FALSE){
			        $data['content'] = ADMINLOGIN; 
					$this->load->view('adminindex', $data);
			    }
			    else{
			    	//Ladda modellen
			    	$this->load->model('credentials_model');

			    	//lägg anv namn o lösen i variablar
			    	$username = $this->input->post('username');
			    	$password = $this->input->post('password');

			    	//anropa modellen för att kolla om 
			    	$query = $this->credentials_model->validate($username, $password);

			    	if($query){
			    		//skapar en array för sessionen
			    		$data = array(
							'username' => $username,
							'is_logged_in' => true
						);
						//initierar sessionen och skickar med arrayen
						$this->session->set_userdata($data);

						//Laddar admin panel vyn
			    		$data['content'] = ADMINPANEL; 
						$this->load->view('adminindex', $data);
			    	}
			    	else{
			    		echo "anv finns inte visa login form igen";
			    		$data['content'] = ADMINLOGIN; 
						$this->load->view('adminindex', $data);
			    	}
			    }
			}
			else{
				$data['content'] = ADMINLOGIN; 
				$this->load->view('adminindex', $data);
			}
		}
		else{
			$data['content'] = ADMINPANEL; 
			$this->load->view('adminindex', $data);
		}
	}

	function list_stories(){
		echo"list stories!";
	}

	function logout()
	{
		$this->session->sess_destroy();
		redirect('/home/index/', 'refresh');
	}	
}